import {Component, OnInit} from '@angular/core';
import {OAuthConfig} from './OAuthConfig';
import {OAuthSession} from './OAuthSession';
import {GnLoginUser, GnMessageService, GnSiteInfo} from 'geminis-ng';
import {HttpClient} from '@angular/common/http';
import {Router} from '@angular/router';

@Component({
  selector: 'portal-logout',
  template: ``
})
export class LogoutComponent implements OnInit {

  constructor(private loginUser: GnLoginUser,
              private siteInfo: GnSiteInfo,
              private http: HttpClient,
              private route: Router,
              private oauthConfig: OAuthConfig,
              private oauthSession: OAuthSession,
              private messageService: GnMessageService) {
  }

  ngOnInit(): void {
    const params = [
      'id_token_hint=' + sessionStorage.getItem('OAuthIdToken'),
      'state=' + this.oauthSession.state
    ];

    this.http.post(this.oauthConfig.oidcConfig.end_session_endpoint, params.join('&'))
      .subscribe((resp: any) => {
        this.route.navigateByUrl(this.siteInfo.loginUrl);
      }, error => {
        this.messageService.error('注销登录失败，' + error.message);
      });
  }
}
